package model.postgresql

import io.getquill.{EntityQuery, query}
import model.events.{
  BaseEvent,
  GroupJoin,
  GroupLeave,
  LikeAdd,
  LikeRemove,
  WallReplyNew,
  WallRepost
}
import zio.json._
import zio.json.ast.Json
import zio.json.internal._

sealed trait PgBaseEvent(event_id: String, timestamp: Long)

object PgBaseEvent {
  given JsonDecoder[PgBaseEvent] = DeriveJsonDecoder.gen[PgBaseEvent]
  given JsonEncoder[PgBaseEvent] = DeriveJsonEncoder.gen[PgBaseEvent]
}

case class PgGroupJoin(
    event_id: String,
    join_type: String,
    timestamp: Long,
    user_id: Long
) extends PgBaseEvent(event_id, timestamp)

object PgGroupJoin {

  def build(groupJoin: GroupJoin): Option[PgGroupJoin] =
    for {
      eventId   <- groupJoin.event_id
      timestamp <- groupJoin.timestamp
    } yield PgGroupJoin(
      eventId,
      groupJoin.`object`.join_type,
      timestamp,
      groupJoin.`object`.user_id
    )
}

case class PgGroupLeave(
    event_id: String,
    self: Long,
    timestamp: Long,
    user_id: Long
) extends PgBaseEvent(event_id, timestamp)

object PgGroupLeave {

  def build(groupLeave: GroupLeave): Option[PgGroupLeave] =
    for {
      eventId   <- groupLeave.event_id
      timestamp <- groupLeave.timestamp
    } yield PgGroupLeave(
      eventId,
      groupLeave.`object`.self,
      timestamp,
      groupLeave.`object`.user_id
    )
}

case class PgLikeAdd(
    event_id: String,
    object_type: String,
    object_id: Long,
    timestamp: Long,
    liker_id: Long
) extends PgBaseEvent(event_id, timestamp)

object PgLikeAdd {

  def build(likeAdd: LikeAdd): Option[PgLikeAdd] =
    for {
      eventId   <- likeAdd.event_id
      timestamp <- likeAdd.timestamp
    } yield PgLikeAdd(
      eventId,
      likeAdd.`object`.object_type,
      likeAdd.`object`.object_id,
      timestamp,
      likeAdd.`object`.liker_id
    )
}

case class PgLikeRemove(
    event_id: String,
    object_type: String,
    object_id: Long,
    timestamp: Long,
    liker_id: Long
) extends PgBaseEvent(event_id, timestamp)

object PgLikeRemove {

  def build(likeRemove: LikeRemove): Option[PgLikeRemove] =
    for {
      eventId   <- likeRemove.event_id
      timestamp <- likeRemove.timestamp
    } yield PgLikeRemove(
      eventId,
      likeRemove.`object`.object_type,
      likeRemove.`object`.object_id,
      timestamp,
      likeRemove.`object`.liker_id
    )
}

case class PgWallReplyNew(
    event_id: String,
    timestamp: Long,
    text: String,
    id: Long,
    from_id: Long,
    post_id: Long
) extends PgBaseEvent(event_id, timestamp)

object PgWallReplyNew {

  def build(wallReplyNew: WallReplyNew): Option[PgWallReplyNew] =
    for {
      eventId   <- wallReplyNew.event_id
      timestamp <- wallReplyNew.timestamp
    } yield PgWallReplyNew(
      eventId,
      timestamp,
      wallReplyNew.`object`.text,
      wallReplyNew.`object`.id,
      wallReplyNew.`object`.from_id,
      wallReplyNew.`object`.post_id
    )

}

case class PgWallRepost(
    event_id: String,
    timestamp: Long,
    text: String,
    id: Long,
    post_type: String,
    from_id: Long,
    `type`: String
) extends PgBaseEvent(event_id, timestamp)

object PgWallRepost {
  def build(wallRepost: WallRepost): Option[PgWallRepost] =
    for {
      eventId   <- wallRepost.event_id
      timestamp <- wallRepost.timestamp
    } yield PgWallRepost(
      eventId,
      timestamp,
      wallRepost.`object`.text,
      wallRepost.`object`.id,
      wallRepost.`object`.post_type,
      wallRepost.`object`.from_id,
      wallRepost.`object`.`type`
    )

}
